programming4us
           
 
 
SQL Server

SQL Server 2008 : Indexing for Performance - Putting It All Together (part 4) - Indexing JOIN Criteria

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/30/2010 3:39:11 PM

5. Indexing JOIN Criteria

Next, you want to retrieve all the orders that contain a certain product. Let's query the SalesOrderDetail table to identify the records. Execute the following code sample. The resulting plan is shown in Figure 9.

SELECT sod.SalesOrderID,sod.SalesOrderDetailID,p.Name,sod.OrderQty
FROM apWriter.SalesOrderDetail sod JOIN apWriter.Product p
ON sod.ProductId = p.ProductId
WHERE p.ProductId = 843

Figure 9. The execution plan of joining the SalesOrderDetail and Product tables

As you review the execution plan, you should see that the bulk of the cost of the query comes from the clustered index scan on the SalesOrderDetail table. If you follow some of the tips discussed in this chapter, then you need to create a nonclustered index on the foreign key within the SalesOrderDetail table.

Since you are also querying the data for the order quantity, you may want to cover the query and include the OrderQty column in the index creation. For demo purposes, Listing 5 creates two indexes: a nonclustered index on just the product ID and a nonclustered index on the product ID that includes the OrderQty column as well.

Example 5. SQL Code That Creates Nonclustered Indexes and Queries Multiple Tables
CREATE NONCLUSTERED INDEX ix_ProductId
ON apWriter.SalesOrderDetail(ProductId)

CREATE NONCLUSTERED INDEX ix_ProductIdInclude
ON apWriter.SalesOrderDetail(ProductId) INCLUDE (OrderQty)

SELECT sod.SalesOrderID,sod.SalesOrderDetailID,p.Name,sod.OrderQty
FROM AdventureWorks2008.apWriter.SalesOrderDetail sod
WITH (index(ix_ProductIdInclude)) JOIN AdventureWorks2008.apWriter.Product p
ON sod.ProductId = p.ProductId
WHERE p.ProductId = 843

SELECT sod.SalesOrderID,sod.SalesOrderDetailID,p.Name,sod.OrderQty
FROM apWriter.SalesOrderDetail sod with(index(ix_ProductId))
JOIN apWriter.Product p on sod.ProductId = p.ProductId
WHERE p.ProductId = 843


After creating the indexes, Listing 5 executes two queries to demonstrate the impact of those indexes. Figure 10 shows the resulting execution plans.

Figure 10. The execution plan of Listing 10-7 using the nonclustered index compared to the nonclustered index with include column

The execution plan in Figure 10 shows the nonclustered index with the include columns, or the first query in the execution plan. The query cost is less because the index covers the query. Pop quiz: Why don't you have to include the SalesOrderId in the index in order to cover the query? Keep in mind that nonclustered indexes contain the clustered index key on the intermediate and leaf level pages. So the query optimizers already have those values and no lookup is required.

Other -----------------
- SQL Server Integration Services : Logged and Nonlogged Operations
- SQL Server Integration Services : Using bcp (part 5)
- SQL Server Integration Services : Using bcp (part 4)
- SQL Server Integration Services : Using bcp (part 3)
- SQL Server Integration Services : Using bcp (part 2) - Fundamentals of Exporting and Importing Data
- SQL Server Integration Services : Using bcp (part 1)
- SQL Server Integration Services : Connection Projects in Visual Studio
- SQL Server Integration Services : The Package Execution Utility (part 3) - The dtutil Utility
- SQL Server Integration Services : The Package Execution Utility (part 2) - Running Packages
- SQL Server Integration Services : The Package Execution Utility (part 1)
- SQL Server Integration Services : The SSIS Designer
- SQL Server Integration Services : Running the SSIS Wizard
- SQL Server Integration Services : A Data Transformation Requirement
- SQL Server 2008 : SSIS Tools and Utilities
- SQL Server 2008 : SSIS Architecture and Concepts
- SQL Server 2008 : SQL Server Integration Services - SSIS Basics
- Defensive Error Handling : Using Transactions and XACT_ABORT to Handle Errors
- Managing Security Within the Database Engine : Securables
- Managing Security Within the Database Engine : Database Security
- Managing Security Within the Database Engine : Creating SQL Server Principals
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us